﻿/*******************************************************************************
 * 
 * 
 * 文件名：FormAlarm.cs
 * 
 * 日  期：2009-05-04
 * 
 * 版  本：1.0.0
 * 
 * Copyright (C) 2009 Gsino
 * 
 * 功  能：报警查询功能
 * 
 * 修改记录
 * 
 * 版本         修改日期            修改人          修改内容
 * 
 * 
 *******************************************************************************/
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using BLL.Spublic;
using BLL.TableFunc;
using System.Collections;

namespace 电力热故障系统
{
    public partial class FormAlarm : Form
    {

        #region 变量

        /// <summary>
        /// 数据集
        /// </summary>
        private DataSet ds;

        /// <summary>
        /// 被选中的区域的id
        /// </summary>
        private ArrayList treeList = new ArrayList();

        /// <summary>
        /// 查询开始时间
        /// </summary>
        private int startTime;

        /// <summary>
        /// 查询结束时间
        /// </summary>
        private int endTime;

        private OtherFunc of = new OtherFunc();

        #endregion

        #region 属性

        public int StartTime
        {
            get { return startTime; }
            set
            {
                startTime = value;
                this.StartDatePicker.Text = of.GetSystemTime(value);
                this.StartTimePicker.Text = "0:00:00";
            }
        }
        public int EndTime
        {
            get { return endTime; }
            set
            {
                endTime = value;
                this.EndTimePicker.Text = "0:00:00";
                this.EndDatePicker.Text = of.GetSystemTime(value);
            }
        }
        public ArrayList TreeList
        {
            get { return treeList; }
            set
            {
                treeList = value;
            }
        }

        #endregion

        #region 构造函数

        public FormAlarm()
        {
            InitializeComponent();
            //初始化查询开始日期
            DateTime dt = DateTime.Now.AddDays(-1.0);
            this.StartDatePicker.Value = dt;
            StartTimePicker.Update();
        }

        #endregion

        #region 退出

        /// <summary>
        /// 退出
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonCancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        #endregion

        # region 查询

        /// <summary>
        /// 查询
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void buttonOK_Click(object sender, EventArgs e)
        {
            //查询开始时间
            startTime = of.GetUnixTime(StartDatePicker.Text + " " + StartTimePicker.Text);
            //查询结束时间
            endTime = of.GetUnixTime(EndDatePicker.Text + " " + EndTimePicker.Text);
            Query();
        }

        public void Query()
        {
            if (treeList.Count == 0)
            {
                MessageBox.Show("请选择要查询的区域", "提示", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                return;
            }

            AlarmTablesFunc af = new AlarmTablesFunc();
            ds = af.selcAlarm(startTime, endTime, treeList);
            DataView dv = ds.Tables[0].DefaultView as DataView;
            dv.AllowNew = false;
            //修改标题
            alarmDataGridView.DataSource = dv;

            alarmDataGridView.Columns[0].HeaderText = "编号";
            alarmDataGridView.Columns[1].HeaderText = "区域名称";
            alarmDataGridView.Columns[2].HeaderText = "数据";
            alarmDataGridView.Columns[3].HeaderText = "报警类型";
            alarmDataGridView.Columns[4].HeaderText = "时间";
            alarmDataGridView.Columns[5].Visible = false;
            //报警类型

            alarmDataGridView.Columns[0].Width = 55;
            alarmDataGridView.Columns[1].Width = 105;
            alarmDataGridView.Columns[2].Width = 55;
            alarmDataGridView.Columns[3].Width = 98;
            alarmDataGridView.Columns[4].Width = 130;

            if (alarmDataGridView.Rows.Count > 0)
                alarmDataGridView[0, 0].Selected = false;

            int m_time = 0;
            string systemTime = null;
            string alarmty = null;
            //数据转换
            for (int i = 0; i < alarmDataGridView.Rows.Count; i++)
            {
                m_time = Convert.ToInt32(alarmDataGridView[4, i].Value);
                systemTime = of.GetSystemTime(m_time);
                alarmDataGridView[4, i].Value = systemTime;

                if (Convert.ToInt32(alarmDataGridView[3, i].Value) == 0)
                    alarmty = "温度";
                if (Convert.ToInt32(alarmDataGridView[3, i].Value) == 1)
                    alarmty = "湿度";
                if (Convert.ToInt32(alarmDataGridView[5, i].Value) == 0)
                    alarmty = alarmty + "上限报警";
                if (Convert.ToInt32(alarmDataGridView[5, i].Value) == 1)
                    alarmty = alarmty + "下限报警";

                if (Convert.ToInt32(alarmDataGridView[3, i].Value) == 0)
                    alarmDataGridView[3, i].Style.BackColor = Color.Red;
                else
                    alarmDataGridView[3, i].Style.BackColor = Color.Yellow;

                alarmDataGridView[3, i].Value = alarmty;
            }
        }

        #endregion

        # region 加载treeview

        /// <summary>
        /// 加载treeview
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void FormAlarm_Load(object sender, EventArgs e)
        {
            //新建父节点
            TreeNode fatherTree = new TreeNode("区域列表");
            panelTreeView.Nodes.Add(fatherTree);
            fatherTree.Name = "0";
            using (DatabaseFunc dbf = new DatabaseFunc())
            {
                string _table = "gs_panel";
                string _sql = "paneladdr,panelname";
                string _term = "1=1 order by paneladdr";

                ds = new DataSet();
                ds = dbf.selcDataInfo(_table, _sql, _term);
                //创建子节点
                int num = 0;
                for (int i = 0; i < ds.Tables[0].Rows.Count; i++)
                {
                    TreeNode childTree = new TreeNode();
                    childTree.Text = ds.Tables[0].Rows[i].ItemArray[1].ToString();
                    childTree.Name = ds.Tables[0].Rows[i].ItemArray[0].ToString();
                    fatherTree.Nodes.Add(childTree);
                    if (treeList.Contains(ds.Tables[0].Rows[i].ItemArray[0]))
                    {
                        childTree.Checked = true;
                        num++;
                    }
                }
                if (num == ds.Tables[0].Rows.Count)
                    fatherTree.Checked = true;
            }
            //展开子节点
            fatherTree.Expand();
        }

        #endregion

        # region treeview 操作

        /// <summary>
        /// 取消节点选中状态之后，取消所有子节点的选中状态
        /// </summary>
        /// <param name="currNode"></param>
        /// <param name="state"></param>
        private void setChildNodeCheckedState(TreeNode currNode, bool state)
        {
            bool _flag = true;
            TreeNodeCollection nodes = currNode.Nodes;
            if (nodes.Count > 0)
                foreach (TreeNode tn in nodes)
                {
                    tn.Checked = state;
                    setChildNodeCheckedState(tn, state);
                }
            if (currNode.Parent != null)
            {
                TreeNodeCollection _nodes = currNode.Parent.Nodes;
                foreach (TreeNode _tn in _nodes)
                {
                    if (_tn.Checked == false)
                    {
                        _flag = false;
                        break;
                    }
                }
                if (_flag == true)
                    currNode.Parent.Checked = true;
            }
        }

        /// <summary>
        /// 取消节点选中状态之后，取消父节点的选中状态
        /// </summary>
        /// <param name="currNode"></param>
        /// <param name="state"></param>
        private void setParentNodeCheckedState(TreeNode currNode, bool state)
        {
            TreeNode parentNode = currNode.Parent;
            parentNode.Checked = state;
        }

        /// <summary>
        /// checkbox选择事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void panelTreeView_AfterCheck(object sender, TreeViewEventArgs e)
        {
            if (e.Action == TreeViewAction.ByMouse)
            {
                if (e.Node.Checked)
                {
                    setChildNodeCheckedState(e.Node, true);
                }
                else
                {
                    //取消节点选中状态之后，取消所有父节点的选中状态
                    setChildNodeCheckedState(e.Node, false);
                    //如果节点存在父节点，取消父节点的选中状态
                    if (e.Node.Parent != null)
                    {
                        setParentNodeCheckedState(e.Node, false);
                    }
                }
            }
            if (e.Node.Checked == true && treeList.Contains(e.Node.Name) == false)//选中后添加到动态数组中  
            {
                treeList.Add(e.Node.Name);
            }
            else if (e.Node.Checked == false && treeList.Contains(e.Node.Name) == true)//取消选中后从动态数组中删除
            {
                treeList.Remove(e.Node.Name);
            }
        }

        private void panelTreeView_AfterSelect(object sender, TreeViewEventArgs e)
        {
            if (e.Node.Checked == true)
            {
                e.Node.Checked = false;
                panelTreeView_AfterCheck(sender, e);
            }
            else
            {
                e.Node.Checked = true;
                panelTreeView_AfterCheck(sender, e);
            }
        }

        #endregion

    }
}